@{
    Validation.ExcludeAll();
}
@{
    //SHOW A SINGLE ARTICLE
    
    // INIT variables ;
	PageData["RenderTwitterHelper"]=false;
    dynamic Comments=null;
    string articleId,title,name,email,message,website,errorMessage,successMessage,published;
    successMessage="";
    // set the view
    var view = "~/Views/Public/_ShowArticle.cshtml";
    // if authenticated , get user id
    var userId = WebSecurity.CurrentUserId;
    // Get ArticleId from querystring
    articleId = UrlData[0].ToString();
    // article null ? redirect
    if(String.IsNullOrEmpty(articleId)){
        Response.Redirect("~/");
    }
    // get requested Article
    if(String.IsNullOrEmpty(articleId)){
        Response.Redirect("~/");
    }
    // check if article exist, if not redirect
    var Article = Blog.DataObject.GetPublishedArticleById(articleId);
   if(Article==null){
        Response.Redirect("~/");
    }
    

    // MANAGE submitted COMMENTS
    // IF A FORM WAS SENT
    if(IsPost){
        
        // get post variables;
        name=Request.Form["name"];
        email=Request.Form["email"];
        title=Request.Form["commentTitle"];
        message=Request.Form["commentBody"];
        website=Request.Form["website"];
        // Check variables
        if(String.IsNullOrEmpty(name)){ModelState.AddFormError("Name cannot be empty");};
        if(String.IsNullOrEmpty(email) || !Utils.IsEmailAdress(email)){ModelState.AddFormError("Email is not valid");};
        if(String.IsNullOrEmpty(title) ){ ModelState.AddFormError("Title cannot be empty"); };
        if(String.IsNullOrEmpty(message) ){ModelState.AddFormError("Message cannot be empty");};
        @*try{
        AntiForgery.Validate();
        }catch(Exception ex){
            ModelState.AddFormError("Error :" + ex);
        }*@
        // if user registered , publish automaticaly
        if(userId<=-1){
    
            published="0";
        }else{
            published="1";
        }
        // IF NO ERROR COMMIT
        if(ModelState.IsValid){
            // trim input
            name=Utils.Trim(name,100);
            email=Utils.Trim(email,200);
            title=Utils.Trim(title,100);
            message=Utils.Trim(message,500,"...");
            website=Utils.Trim(website,300);
            // COMMIT
            var query = Blog.DataObject.AddComment(name,title,email,message,website,articleId,userId.ToString(),published);
            if(query>0){
                // validation success
                successMessage ="Your comment have been submitted , wait for its validation";
            }else{
                // query failed
                ModelState.AddFormError("Comment not submitted");
            }
        }
        
    }
  
    // get comments from requested article
    // at the end to get comments autmotatically validated ones in display after a comment insertion
    if(Article.ShowComments == 1){
       Comments = Blog.DataObject.GetPublishedCommentsByArticleId(Article.id.ToString());
    }
}

@RenderPage(view,new{article=Article,Comments=Comments,errorMessage = Html.ValidationSummary(),successMessage=successMessage})